import request from '@/config/axios'
import type { RoleData, RoleMenuData } from './types'

export const getRoleListApi = (params: any): Promise<IResponse> => {
    const queryParams = params.queryParams ? params.queryParams : {}
    if (params.queryParams) {
        delete params.queryParams
    }
    Object.assign(params, queryParams)
    params.pageNum = params.page_index
    params.pageSize = params.page_size
    delete params.page_index
    delete params.page_size
    return request.get({url: '/auth/role/page', params})
}

export const saveOrUpdate = (data: Partial<RoleData>, isUpdate = false): Promise<IResponse> => {
    if (isUpdate) {
        return request.put({url: '/auth/role/', data})
    } else {
        return request.post({url: '/auth/role/', data})
    }
}

export const getRoleDetailApi = (id: string | number): Promise<IResponse<RoleData>> => {
    return request.get({url: `/auth/role/${id}`})
}

export const delRoleApi = (data: Partial<RoleData>[]): Promise<IResponse> => {
    if (data.length > 0) {
        const ids = data.map((d) => d.id);
        return request.delete({ url: `/auth/role/delete/${ids.join(',')}` })
    } else {
        const id = data[0].id
        return request.delete({ url: `/auth/role/${id}` })
    }
}

export const getPermission = (roleId: string | number): Promise<IResponse> => {
    return request.get({url: `/auth/permission/${roleId}`})
}